package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.common.response.JsonResult;
import cn.tedu._05weibo.common.response.StatusCode;
import cn.tedu._05weibo.mapper.CommentMapper;
import cn.tedu._05weibo.pojo.dto.CommentDTO;
import cn.tedu._05weibo.pojo.entity.Comment;
import cn.tedu._05weibo.pojo.vo.CommentVO;
import cn.tedu._05weibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "03.评论管理模块")
@RestController
@RequestMapping("/v1/comment")
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    @ApiOperation("评论")
    public JsonResult insertComment(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");
        /*if (userVO == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        }*/
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setCreated(new Date());
        comment.setUserId(userVO.getId());
        commentMapper.insertComment(comment);
        return JsonResult.ok();
    }

    @GetMapping("selectByWeiboId")
    @ApiOperation("根据微博id查询评论")
    @ApiImplicitParam(name = "id", value = "微博id", required = true, dataType = "int")
    public JsonResult selectByWeiboId(Integer id) {
        if (id < 0){
            throw new IllegalArgumentException("微博id不能小于0");
        }
        return JsonResult.ok(commentMapper.selectCommentByWeiboId(id));
    }

}
